package com.xmmj.common.refresh

import com.xmmj.baselibrary.utils.Unit.dp2px
import kotlin.jvm.JvmOverloads
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt

/**
 * 3个旋转小球
 */
class ThreeBallView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    //画笔
    private var mPaint: Paint? = null

    //球的最大半径
    private var maxRadius = dp2px(getContext(), 6f).toFloat()

    //球的最小半径
    private var minRadius = 0f

    //小球旋转的范围距离
    private var distance = dp2px(getContext(), 15f)

    //动画的时间
    private var duration = DEFAULT_ANIMATOR_DURATION.toLong()

    private var mOneBall: Ball? = null

    private var mTwoBall: Ball? = null

    private var mThreeBall: Ball? = null

    private var mCenterX = 0f

    private var mCenterY = 0f

    private var animatorSet: AnimatorSet? = null

    companion object {
        //默认第一个小球颜色
        private val DEFAULT_ONE_BALL_COLOR = Color.parseColor("#fbc31f")

        //默认第二个小球颜色
        private val DEFAULT_TWO_BALL_COLOR = Color.parseColor("#3479F7")

        //默认第三个小球颜色
        private val DEFAULT_THREE_BALL_COLOR = Color.parseColor("#888888")

        //默认动画执行时间
        private const val DEFAULT_ANIMATOR_DURATION = 1000
    }

    init {
        init(context)
    }

    private fun init(context: Context) {
        mOneBall = Ball()
        mTwoBall = Ball()
        mThreeBall = Ball()
        mOneBall!!.color = DEFAULT_ONE_BALL_COLOR
        mTwoBall!!.color = DEFAULT_TWO_BALL_COLOR
        mThreeBall!!.color = DEFAULT_THREE_BALL_COLOR
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        configAnimator()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mCenterX = (w / 2).toFloat()
        mCenterY = (h / 2).toFloat()
        mOneBall!!.centerX = mCenterX - distance
        mOneBall!!.radius = maxRadius * 0.7f
        mTwoBall!!.centerX = mCenterX
        mTwoBall!!.radius = maxRadius
        mThreeBall!!.centerX = mCenterX + distance
        mThreeBall!!.radius = maxRadius * 0.7f
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mCenterX = (width / 2).toFloat()
        mCenterY = (height / 2).toFloat()
    }

    override fun onDraw(canvas: Canvas) {
        mPaint!!.color = mOneBall!!.color
        canvas.drawCircle(mOneBall!!.centerX, mCenterY, mOneBall!!.radius, mPaint!!)
        mPaint!!.color = mTwoBall!!.color
        canvas.drawCircle(mTwoBall!!.centerX, mCenterY, mTwoBall!!.radius, mPaint!!)
        mPaint!!.color = mThreeBall!!.color
        canvas.drawCircle(mThreeBall!!.centerX, mCenterY, mThreeBall!!.radius, mPaint!!)
    }

    /**
     * 配置属性动画
     */
    @SuppressLint("ObjectAnimatorBinding")
    private fun configAnimator() {
        //中间半径大小，取最大的70%
        val centerRadius = maxRadius * 0.7f

        //第一个小球缩放动画，通过改变小球的半径
        //半径变化规律：中间大小->最大->中间大小->最小->中间大小
        val oneScaleAnimator = ObjectAnimator.ofFloat(
            mOneBall, "radius",
            centerRadius, maxRadius, centerRadius, minRadius, centerRadius
        )
        //无限循环
        oneScaleAnimator.repeatCount = ValueAnimator.INFINITE

        //第一个小球位移动画，通过改变小球的圆心
        val oneCenterAnimator = ValueAnimator.ofFloat(-1f, 0f, 1f, 0f, -1f)
        oneCenterAnimator.repeatCount = ValueAnimator.INFINITE
        oneCenterAnimator.addUpdateListener { valueAnimator: ValueAnimator ->
            val value = valueAnimator.animatedValue as Float
            val x = mCenterX + distance * value
            mOneBall!!.centerX = x
            //不停的刷新view，让view不停的重绘
            invalidate()
        }

        //第二个小球缩放动画
        //变化规律：最大->中间大小->最小->中间大小->最大
        val twoScaleAnimator = ObjectAnimator.ofFloat(
            mTwoBall,
            "radius",
            maxRadius,
            centerRadius,
            minRadius,
            centerRadius,
            maxRadius
        )
        twoScaleAnimator.repeatCount = ValueAnimator.INFINITE

        //第二个小球位移动画
        val twoCenterAnimator = ValueAnimator.ofFloat(0f, 1f, 0f, -1f, 0f)
        twoCenterAnimator.repeatCount = ValueAnimator.INFINITE
        twoCenterAnimator.addUpdateListener { valueAnimator: ValueAnimator ->
            val value = valueAnimator.animatedValue as Float
            val x = mCenterX + distance * value
            mTwoBall!!.centerX = x
        }

        //第三个小球缩放动画
        //变化规律：中间大小->最小->中间大小->最大->中间大小
        val threeScaleAnimator = ObjectAnimator.ofFloat(
            mThreeBall, "radius", centerRadius, minRadius,
            centerRadius, maxRadius, centerRadius
        )
        threeScaleAnimator.repeatCount = ValueAnimator.INFINITE

        //第三个小球位移动画
        val threeCenterAnimator = ValueAnimator.ofFloat(1f, 0f, -1f, 0f, 1f)
        threeCenterAnimator.repeatCount = ValueAnimator.INFINITE
        threeCenterAnimator.addUpdateListener { valueAnimator: ValueAnimator ->
            val value = valueAnimator.animatedValue as Float
            val x = mCenterX + distance * value
            mThreeBall!!.centerX = x
        }

        //属性动画集合
        animatorSet = AnimatorSet()
        //六个属性动画一块执行
        animatorSet!!.playTogether(
            oneScaleAnimator,
            oneCenterAnimator,
            twoScaleAnimator,
            twoCenterAnimator,
            threeScaleAnimator,
            threeCenterAnimator
        )
        //动画一次运行时间
        animatorSet!!.duration = duration
    }

    /**
     * 小球
     */
    inner class Ball {
        var radius //半径
                = 0f
        var centerX //圆心
                = 0f
        var color //颜色
                = 0
    }

    /**
     * 设置第一个球的颜色
     *
     * @param color
     */
    fun setOneBallColor(@ColorInt color: Int) {
        mOneBall!!.color = color
    }

    /**
     * 设置第二个球的颜色
     *
     * @param color
     */
    fun setmTwoBallColor(@ColorInt color: Int) {
        mTwoBall!!.color = color
    }

    /**
     * 设置球的最大半径
     *
     * @param maxRadius
     */
    fun setMaxRadius(maxRadius: Float) {
        this.maxRadius = maxRadius
        configAnimator()
    }

    /**
     * 设置球的最小半径
     *
     * @param minRadius
     */
    fun setMinRadius(minRadius: Float) {
        this.minRadius = minRadius
        configAnimator()
    }

    /**
     * 设置两个球旋转的最大范围距离
     *
     * @param distance
     */
    fun setDistance(distance: Int) {
        this.distance = distance
    }

    fun setDuration(duration: Long) {
        this.duration = duration
        if (animatorSet != null) {
            animatorSet!!.duration = duration
        }
    }

    /**
     * 开始动画
     */
    fun startAnimator() {
        if (visibility != VISIBLE) return
        if (animatorSet!!.isRunning) return
        if (animatorSet != null) {
            animatorSet!!.start()
        }
    }

    /**
     * 结束停止动画
     */
    fun stopAnimator() {
        if (animatorSet != null) {
            animatorSet!!.end()
        }
    }
}